<template>
  <component is="a-modal" v-model:visible="visible" :width="1000" :title="title" :mask-closable="false"
    :ok-loading="loading" @cancel="close" @before-ok="submit">
    <!-- 表单信息 start -->
    <a-form ref="formRef" :model="formData" :rules="rules" :auto-label-width="true" style="height: 70vh;">
      <a-form-item label="SKU" field="sku">
        <a-input :disabled="mode=='edit'" v-model="formData.sku" placeholder="4-25位, 添加后不可修改" :min-length="4" :max-length="25" />
      </a-form-item>
      <a-form-item label="SKU类型" field="type">
        <sa-select :data="paramsData.type" v-model="formData.type"></sa-select>
      </a-form-item>
      <a-form-item label="商品型号" field="model">
        <a-input :disabled="mode=='edit'" v-model="formData.model" placeholder="4-25位, 添加后不可修改" :min-length="4" :max-length="25" />
      </a-form-item>
      <!-- <a-form-item label="库存数量" field="stock">
        <a-input-number mode="number" v-model="formData.stock" placeholder="请输入" />
      </a-form-item> -->
      <a-form-item label="库存预警数" field="num_alert">
        <a-input-number mode="number" v-model="formData.num_alert" placeholder="请输入" />
      </a-form-item>
      <a-form-item label="是否超售" field="oversold">
        <sa-radio v-model="formData.oversold" :data="ENUMS.IS_CAN"></sa-radio>
      </a-form-item>
      <a-form-item label="产品名称(中文)" field="name">
        <a-input v-model="formData.name" placeholder="请输入" />
      </a-form-item>
      <a-form-item label="产品名称(英文)" field="name_en">
        <a-input v-model="formData.name_en" placeholder="请输入" />
      </a-form-item>
      <a-form-item label="海关编码" field="customs_code">
        <a-input v-model="formData.customs_code" placeholder="请输入" />
      </a-form-item>
      <a-form-item label="产品类型" field="goods_type">
        <sa-select :data="paramsData.goods_type" v-model="formData.goods_type"></sa-select>
      </a-form-item>
      <a-form-item label="状态" field="state">
        <sa-radio v-model="formData.state" :data="ENUMS.STATE"></sa-radio>
      </a-form-item>
      <a-form-item label="封面图" field="image">
        <sa-upload-image v-model="formData.image" :limit="1"></sa-upload-image>
      </a-form-item>

    </a-form>
    <!-- 表单信息 end -->
  </component>
</template>

<script setup>
import { ref, reactive, computed, watch } from 'vue'
import { Message } from '@arco-design/web-vue'
import mallApi from '@/api/system/mall'
import ENUMS from '@/utils/enums'
import SaSelect from '@cps/sa-select/index.vue'
import SaRadio from '@cps/sa-radio/index.vue'
import SaUploadImage from '@cps/sa-upload-image/index.vue'
import { throttle } from 'lodash'
import tool from '@/utils/tool'
const paramsData = ref({})
// 获取角色组列表
const getParams = async () => {
  const { data } = await mallApi.addSkuParams()
  paramsData.value = { ...data }
  paramsData.value.goods_type = tool.simpleTransformArray(data.goods_type)
  paramsData.value.type = tool.toArray_value(data.type)
}
const emit = defineEmits(['success'])
console.log('ENUMS', ENUMS.STATE);
// 引用定义
const formRef = ref()
const mode = ref('')
const visible = ref(false)
const loading = ref(false)

let title = computed(() => {
  return (mode.value == 'add' ? '新增' : '修改')
})
// 表单信息
const formData = reactive({
  sku: "",
  type: 0,
  model: "",
  // stock: 0,
  num_alert: 0,
  oversold: 1,
  name: '',
  name_en: '',
  customs_code: " ",
  goods_type: null,
  state: 1,
  file: " ",
  image: '',
  id: " ",
})

// 验证规则
const rules = ref({
  sku: [{ required: true, message: '请输入' }],
  type: [{ required: true, message: '请选择' }],
  model: [{ required: true, message: '请输入' }],
  // stock: [{ required: true, message: '请输入' }],
  num_alert: [{ required: true, message: '请输入' }],
  name: [{ required: true, message: '请输入' }],
  name_en: [{ required: true, message: '请输入' }],
  goods_type: [{ required: true, message: '请选择' }],
  state: [{ required: true, message: '请选择' }],
  file: [{ required: true, message: '请上传' }],
  oversold: [{ required: true, message: '请选择' }],
});
// 打开弹框
const open = async (type = 'add', id = '') => {
  mode.value = type
  visible.value = true
  formRef.value?.resetFields()
  await initPage()
  await getParams()
  if (type == 'add') {

  } else {
    formData.id = id
  }
}

// 初始化页面数据
const initPage = async () => {
}

// 设置数据
const setFormData = async (data) => {
  for (const key in formData) {
    if (data[key] != null && data[key] != undefined) {
      formData[key] = data[key]
    }
  }
}

// 数据保存
const submit = throttle(async (done) => {
  const validate = await formRef.value?.validate()
  if (!validate) {
    loading.value = true
    try {
      let data = { ...formData }
      let result = {}
      if (mode.value === 'add') {
        // 添加数据
        data.id = undefined
        result = await mallApi.addSku(data)
      } else {
        // 修改数据
        result = await mallApi.updateSku(data)
      }
      if (result.code === 200) {
        Message.success(result.msg||'操作成功')
        emit('success')
        done(true)
      }
      // 防止连续点击提交
      setTimeout(() => {
        loading.value = false
      }, 500)
    } catch {
      loading.value = false
    }
  }
  done(false)
}, 100)

// 关闭弹窗
const close = () => (visible.value = false)

defineExpose({ open, setFormData })
</script>
